package com.loong.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.loong.mapper.UserMapper;
import com.loong.pojo.User;
import com.loong.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private UserMapper userMapper;

    @Override
    @Transactional
    public void register(String username, String password, String confirmPassword) {
        if (!password.equals(confirmPassword)) {
            throw new RuntimeException("两次密码不一致");
        }
        // 密码加密
        String encodedPwd = passwordEncoder.encode(password);
        User user = new User();
        user.setUsername(username);
        user.setPassword(encodedPwd);
        userMapper.insert(user);
        // 分配权限
        userMapper.addRole(user.getUid(), 1L);
    }
}